package com.example.deertrip;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.deertrip.R;
import com.example.deertrip.bean.Tb_HotelInfo;
import com.example.deertrip.bean.Tb_HotelReserve;
import com.example.deertrip.database.DBOperate;
import com.example.deertrip.tools_class.DrawableHelp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderActivity extends AppCompatActivity {
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private ListView listView;
    private SimpleAdapter simpleAdapter = null;
    private DBOperate dbOperate;
    private  SimpleAdapter adapter=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        listView=findViewById(R.id.htorderlist);
        dbOperate=new DBOperate(OrderActivity.this);

        sql();
    }

    private void sql() {
        List<Tb_HotelReserve> ReserveInfos = dbOperate.get_All_ReserveInfo();
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
        Drawable dw;

        for (int i = 0; i < ReserveInfos.size(); i++) {
            HashMap<String, Object> items = new HashMap<String, Object>(); //存放一条数据的哈希表


            Tb_HotelReserve ReserveInfo = ReserveInfos.get(i);

            items.put("HotelName", ReserveInfo.getHotelName());
            items.put("RoomNumber", ReserveInfo.getRoomNumber());
            items.put("HotelOrderNum", ReserveInfo.getHotelOrderNum());
            items.put("HotelOrderPrice", ReserveInfo.getHotelOrderPrice());
            items.put("CheckinDate", ReserveInfo.getCheckinDate());
            items.put("CheckoutDate", ReserveInfo.getCheckoutDate());
            items.put("RoomType", ReserveInfo.getRoomType());
            items.put("UserName", ReserveInfo.getUserName());
            items.put("id",ReserveInfo.getHotelOrderNum());

            data.add(items);   //总数据哈希表中加入该条数据项

        }


        adapter = new SimpleAdapter(OrderActivity.this, data, R.layout.hotel_order_bar,
                new String[]{"HotelName", "HotelOrderNum", "HotelOrderPrice", "CheckinDate", "UserName"}, new int[]{R.id.hotname, R.id.hotordernum, R.id.hotorderprice, R.id.lindate, R.id.hotperson})
        {

                @Override
                public View getView ( int position, View convertView, ViewGroup parent){
                final int p = position;
                Button deletebtn;// id为你自定义布局中按钮的id
                convertView = super.getView(position, convertView, parent);
                deletebtn = convertView.findViewById(R.id.td);
                deletebtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        HashMap<String, Object> items = new HashMap<String, Object>();
                        items = (HashMap<String, Object>) getItem(p);
                        Integer id = (Integer) items.get("id");
                        System.out.println(id);
                        System.out.println("点击了第" + p + "项");
                        dbOperate.delete_order(id);
                        data.remove(p);
                        adapter.notifyDataSetChanged();


                        Integer diff= (Integer) items.get("HotelOrderPrice");

                        SharedPreferences sp=getSharedPreferences("deertrip",MODE_PRIVATE);
                        SharedPreferences.Editor editor=sp.edit();
                        String money=sp.getString("WxBalance","0");
                        Integer newmoney=diff+Integer.valueOf(money).intValue();
                        System.out.println(newmoney);
                        editor.putString("WxBalance",String.valueOf(newmoney));
                        editor.commit();
                        Toast.makeText(OrderActivity.this, "退订成功！", Toast.LENGTH_LONG).show();
                    }
                });

                return convertView;
            }



        };

        listView.setAdapter(adapter);

    }
}